var data = require("./data.js");
var clickjs = require("./click.js");
var mousejs = require("./mouse.js");

var define = data.define;
var gas = [];

var gmouse = [];

function createGAS(map, mapW, mapH, width, mapMouse) {
  if (!clickjs.canInput(map, mapW, mapH, width, mapMouse))
    return;

  var t = {};
  t.x = parseInt(clickjs.getX());
  t.y = parseInt(clickjs.getY());
  t.times = 0;
  t.width = 1;
  t.sx = parseInt(Math.max(t.x - t.width / 2, 1));
  t.sy = parseInt(Math.max(t.y - t.width / 2, 1));
  t.ex = Math.min(t.sx + t.width, mapH - 2);
  t.ey = Math.min(t.sy + t.width, mapW - 2);
  gas.push(t);
  calBound(gas.length - 1, map, mapW, mapH, width, mapMouse);
  showGAS(t, map, mapW, mapH, width, mapMouse);
}

function calBound(n, map, mapW, mapH, width, mapMouse) {
  gas[n].sx = parseInt(Math.max(gas[n].x - gas[n].width / 2, 1));
  gas[n].sy = parseInt(Math.max(gas[n].y - gas[n].width / 2, 1));
  gas[n].ex = Math.min(gas[n].sx + gas[n].width, mapH - 2);
  gas[n].ey = Math.min(gas[n].sy + gas[n].width, mapW - 2);
}

function showGAS(b, map, mapW, mapH, width, mapMouse) {
  for (var i = b.sx; i <= b.ex; i++) {
    for (var j = b.sy; j <= b.ey; j++) {
      if (!data.isWall(map[i][j]) && map[i][j] != define.Block) {
        map[i][j] = define.GAS;
      }
    }
  }
}

function clearGAS(b, map, mapW, mapH, width, mapMouse) {
  for (var i = b.sx; i <= b.ex; i++) {
    for (var j = b.sy; j <= b.ey; j++) {
      if (map[i][j] == define.GAS) {
        map[i][j] = 0;
      }
    }
  }
}

function addGMouse(label, map, mapW, mapH, width, mapMouse) {
  for (var i = 0; i < gmouse.length; i++) {
    if (gmouse[i].valid && gmouse[i].label == label) {
      gmouse[i].times++;
      if (gmouse[i].times > define.PosionTime) {
        gmouse[i].valid = false;
        mousejs.delete_mouse(gmouse[i].label, map, mapW, mapH, width, mapMouse);
        console.log("Gas: mouse!");
      }
      return;
    }
  }
  var temp = {};
  temp.valid = true;
  temp.times = 0;
  temp.label = label;
  gmouse.push(temp);
}

function detectMouse(b, map, mapW, mapH, width, mapMouse) {
  for (var i = b.sx; i <= b.ex; i++) {
    for (var j = b.sy; j <= b.ey; j++) {
      if (mapMouse[i][j] != -1) {
        addGMouse(mapMouse[i][j], map, mapW, mapH, width, mapMouse);
      }
    }
  }
}

function updateGAS(map, mapW, mapH, width, mapMouse) {
  var temp = [];
  for (var i = 0; i < gas.length; i++) {
    gas[i].times++;
    if (gas[i].times >= define.WapGASTime / define.ShowDelay) {
      clearGAS(gas[i], map, mapW, mapH, width, mapMouse);
    }
    else {
      detectMouse(gas[i], map, mapW, mapH, width, mapMouse);
      gas[i].width = Math.min(define.MaxGASWidth, gas[i].times * define.ShowDelay / 1000);
      calBound(i, map, mapW, mapH, width, mapMouse);
      showGAS(gas[i], map, mapW, mapH, width, mapMouse);
      temp.push(gas[i]);
    }
  }
  gas = temp;
}

function getNum() {
  return gas.length;
}

function clear() {
  gas = [];
  gmouse = [];
}

function getInfo(map) {
  let temp = [];
  for (var n = 0; n < gas.length; n++) {
    var b = gas[n];
    for (var i = b.sx; i <= b.ex; i++) {
      for (var j = b.sy; j <= b.ey; j++) {
        if (map[i][j] == define.GAS) {
          var t = {};
          t.x = i;
          t.y = j;
          temp.push(t);
        }
      }
    }
  }
  return temp;
}

module.exports = {
  createGAS: createGAS,
  updateGAS: updateGAS,
  getNum: getNum,
  clear: clear,
  getInfo: getInfo
}